package org.mokusakura.web.interceptor;

import org.mokusakura.constant.ServletUrlConstant;
import org.mokusakura.constant.SessionAttributeKeyConstant;
import org.mokusakura.persistence.Util.RedirectUtil;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Component
public class LoginFirstInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws
            Exception {
        if (request.getSession().getAttribute(SessionAttributeKeyConstant.LOGIN_USER) == null) {
            RedirectUtil.sendRedirect(request, response, ServletUrlConstant.LOGIN_PAGE_SERVLET_URL, false);
            return false;
        }
        return true;
    }
}
